package com.estate.system.service.impl;

import com.estate.common.constant.UserConstants;
import com.estate.common.core.text.Convert;
import com.estate.common.exception.ServiceException;
import com.estate.common.utils.StringUtils;
import com.estate.system.domain.SysAdvantage;
import com.estate.system.domain.SysPost;
import com.estate.system.mapper.SysAdvantageMapper;
import com.estate.system.mapper.SysPostMapper;
import com.estate.system.mapper.SysUserPostMapper;
import com.estate.system.service.ISysAdvantageService;
import com.estate.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位信息 服务层处理
 * 
 * @author estate
 */
@Service
public class SysAdvantageServiceImpl implements ISysAdvantageService
{
    @Autowired
    private SysAdvantageMapper advantageMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    /**
     * 查询岗位信息集合
     * 
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysAdvantage> selectAdvantageList(SysAdvantage advantage)
    {
        return advantageMapper.selectAdvantageList(advantage);
    }

    /**
     * 查询所有岗位
     * 
     * @return 岗位列表
     */
    @Override
    public List<SysAdvantage> selectAdvantageAll()
    {
        return advantageMapper.selectAdvantageAll();
    }

    /**
     * 根据用户ID查询岗位
     * 
     * @param userId 用户ID
     * @return 岗位列表
     */
    @Override
    public List<SysAdvantage> selectAdvantagesByUserId(Long userId)
    {
        List<SysAdvantage> userAdvantages = advantageMapper.selectAdvantagesByUserId(userId);
        List<SysAdvantage> advantages = advantageMapper.selectAdvantageAll();
        for (SysAdvantage advantage : advantages)
        {
            for (SysAdvantage userRole : userAdvantages)
            {
                if (advantage.getAdvantageId().longValue() == userRole.getAdvantageId().longValue())
                {
                    advantage.setFlag(true);
                    break;
                }
            }
        }
        return advantages;
    }

    /**
     * 通过岗位ID查询岗位信息
     * 
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysAdvantage selectAdvantageById(Long advantageId)
    {
        return advantageMapper.selectAdvantageById(advantageId);
    }

    /**
     * 批量删除岗位信息
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAdvantageByIds(String ids)
    {
        Long[] advantageIds = Convert.toLongArray(ids);
        for (Long advantageId : advantageIds)
        {
            SysAdvantage advantage = selectAdvantageById(advantageId);
            if (countUserAdvantageById(advantageId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", advantage.getAdvantageName()));
            }
        }
        return advantageMapper.deleteAdvantageByIds(advantageIds);
    }

    /**
     * 新增保存岗位信息
     * 
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int insertAdvantage(SysAdvantage advantage)
    {
        return advantageMapper.insertAdvantage(advantage);
    }

    /**
     * 修改保存岗位信息
     * 
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int updateAdvantage(SysAdvantage advantage)
    {
        return advantageMapper.updateAdvantage(advantage);
    }

    /**
     * 通过岗位ID查询岗位使用数量
     * 
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserAdvantageById(Long advantageId)
    {
        return userPostMapper.countUserPostById(advantageId);
    }

    /**
     * 校验岗位名称是否唯一
     * 
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public String checkAdvantageNameUnique(SysAdvantage advantage)
    {
        ///
        Long postId = StringUtils.isNull(advantage.getAdvantageId()) ? -1L : advantage.getAdvantageId();
        SysAdvantage info = advantageMapper.checkAdvantageNameUnique(advantage.getAdvantageName());
        if (StringUtils.isNotNull(info) && info.getAdvantageId().longValue() != postId.longValue())
        {
            return UserConstants.POST_NAME_NOT_UNIQUE;
        }
        return UserConstants.POST_NAME_UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */

}
